package com.jast.business.controller;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jast.business.domain.BigdataHbaseKvLine;
import com.jast.business.param.BigdataHbaseKvLineParam;
import com.jast.business.param.BigdataHdfsKvLineParam;
import com.jast.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jast.common.annotation.Log;
import com.jast.common.enums.BusinessType;
import com.jast.business.domain.BigdataHdfsKvLine;
import com.jast.business.service.IBigdataHdfsKvLineService;
import com.jast.common.core.controller.BaseController;
import com.jast.common.core.domain.AjaxResult;
import com.jast.common.utils.poi.ExcelUtil;
import com.jast.common.core.page.TableDataInfo;

/**
 * HDFS监控指标近3天指标Controller
 * 
 * @author Jast
 * @date 2023-02-08
 */
@Controller
@RequestMapping("/business/hdfsLine")
public class BigdataHdfsKvLineController extends BaseController
{
    private String prefix = "business/hdfsLine";

    @Autowired
    private IBigdataHdfsKvLineService bigdataHdfsKvLineService;

    @RequiresPermissions("business:hdfsLine:view")
    @GetMapping()
    public String hdfsLine()
    {
        return prefix + "/hdfsLine";
    }

    /**
     * 查询HDFS监控指标近3天指标列表
     */
    @RequiresPermissions("business:hdfsLine:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BigdataHdfsKvLine bigdataHdfsKvLine)
    {
        startPage();
        List<BigdataHdfsKvLine> list = bigdataHdfsKvLineService.selectBigdataHdfsKvLineList(bigdataHdfsKvLine);
        return getDataTable(list);
    }

    /**
     * 导出HDFS监控指标近3天指标列表
     */
    @RequiresPermissions("business:hdfsLine:export")
    @Log(title = "HDFS监控指标近3天指标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BigdataHdfsKvLine bigdataHdfsKvLine)
    {
        List<BigdataHdfsKvLine> list = bigdataHdfsKvLineService.selectBigdataHdfsKvLineList(bigdataHdfsKvLine);
        ExcelUtil<BigdataHdfsKvLine> util = new ExcelUtil<BigdataHdfsKvLine>(BigdataHdfsKvLine.class);
        return util.exportExcel(list, "HDFS监控指标近3天指标数据");
    }

    /**
     * 新增HDFS监控指标近3天指标
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存HDFS监控指标近3天指标
     */
    @RequiresPermissions("business:hdfsLine:add")
    @Log(title = "HDFS监控指标近3天指标", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BigdataHdfsKvLine bigdataHdfsKvLine)
    {
        return toAjax(bigdataHdfsKvLineService.insertBigdataHdfsKvLine(bigdataHdfsKvLine));
    }


    /**
     * 更新/新增HDFS监控指标近3天指标
     */
    @Log(title = "HDFS监控指标近3天指标", businessType = BusinessType.UPDATE)
    @PostMapping("/upsert")
    @ResponseBody
    public AjaxResult upsert(BigdataHdfsKvLine bigdataHdfsKvLine)
    {
        return toAjax(bigdataHdfsKvLineService.upsertBigdataHdfsKvLine(bigdataHdfsKvLine));
    }

    /**
     * 修改HDFS监控指标近3天指标
     */
    @RequiresPermissions("business:hdfsLine:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BigdataHdfsKvLine bigdataHdfsKvLine = bigdataHdfsKvLineService.selectBigdataHdfsKvLineById(id);
        mmap.put("bigdataHdfsKvLine", bigdataHdfsKvLine);
        return prefix + "/edit";
    }

    /**
     * 修改保存HDFS监控指标近3天指标
     */
    @RequiresPermissions("business:hdfsLine:edit")
    @Log(title = "HDFS监控指标近3天指标", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BigdataHdfsKvLine bigdataHdfsKvLine)
    {
        return toAjax(bigdataHdfsKvLineService.updateBigdataHdfsKvLine(bigdataHdfsKvLine));
    }

    /**
     * 删除HDFS监控指标近3天指标
     */
    @RequiresPermissions("business:hdfsLine:remove")
    @Log(title = "HDFS监控指标近3天指标", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bigdataHdfsKvLineService.deleteBigdataHdfsKvLineByIds(ids));
    }

    /**
     * 查询Hbase监控指标最新值列表
     */
    @PostMapping("/getBasicInfo")
    @ResponseBody
    public AjaxResult getBasicInfo(BigdataHdfsKvLineParam bigdataHbaseKvLineParam)
    {
        System.out.println("请求参数:"+ JSON.toJSONString(bigdataHbaseKvLineParam));

        List<BigdataHdfsKvLine> bigdataHbaseKvLines = bigdataHdfsKvLineService.selectBigdataHdfsKvLineList(bigdataHbaseKvLineParam);
        System.out.println("bsize:"+bigdataHbaseKvLines.size());
        JSONObject jsonObject = new JSONObject();
        //类型->机器->值
        JSONObject timeJsonObject = new JSONObject();
        for (BigdataHdfsKvLine bigdataHbaseKvLine : bigdataHbaseKvLines) {
            String htype = bigdataHbaseKvLine.getHtype();
            String hkey = bigdataHbaseKvLine.getHkey();
            String hvalue = bigdataHbaseKvLine.getHvalue();
            String node = bigdataHbaseKvLine.getNode();
            String monitorTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,bigdataHbaseKvLine.getMonitorTime());
            String type = htype + hkey;
            if(jsonObject.containsKey(type)){
                JSONObject jsonObject1 = jsonObject.getJSONObject(type);
                if(jsonObject1.containsKey("time")){
                    JSONArray time = jsonObject1.getJSONArray("time");
                    if(!time.contains(monitorTime)) {
                        time.add(monitorTime.toString());
                    }
                }else{
                    JSONArray time = new JSONArray();
                    time.add(monitorTime);
                    jsonObject1.put("time",time);
                }
                if(jsonObject1.containsKey(node)){
                    JSONArray jsonArray = jsonObject1.getJSONArray(node);
                    jsonArray.add(hvalue);
                    jsonObject1.put(node,jsonArray);
                }else{
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(hvalue);
                    jsonObject1.put(node,jsonArray);
                }
                jsonObject.put(type,jsonObject1);
            }else{
                JSONObject jsonObject1 = new JSONObject();
                JSONArray jsonArray = new JSONArray();
                jsonObject1.put(node,jsonArray);
                jsonArray.add(hvalue);
                jsonObject.put(type,jsonObject1);
            }
        }
        System.out.println("请求结果："+jsonObject.toJSONString());
        return success(jsonObject);
    }
}
